/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.coba.killthispage;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;

/**
 * @author Abahe
 */
public class KillThisPageMIDlet extends MIDlet implements CommandListener {

    protected Display display;      //reference to display
    private ScreenOne screenOne;
    private ScreenTwo screenTwo;
    private ScreenThree screenThree;
    private ScreenFour screenFour;
    private ScreenFive screenFive;
    private Command cmdExit;
    private Command cmdNext;
    private int currentPage = 0;

    public KillThisPageMIDlet() {
        display = Display.getDisplay(this);

        cmdNext = new Command("Next", Command.SCREEN, 0);
        cmdExit = new Command("Exit", Command.EXIT, 0);

        // create game canvas and exit command
    }

    public void startApp() {
        showScreen(1);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        screenOne = null;
        screenTwo = null;
        screenThree = null;
        screenFour = null;
        screenFive = null;
    }

    public void beforeShow(int no) {
        if (no == 1) {
            screenOne.stop();
            screenOne = null;
            showScreen(2);
        } else if (no == 2) {
            screenTwo.stop();
            screenTwo = null;
            showScreen(3);
        } else if (no == 3) {
            screenThree.stop();
            screenThree = null;
            showScreen(4);
        } else if (no == 4) {
            screenFour.stop();
            screenFour = null;
            showScreen(5);
        } else if (no == 5) {
            screenFive.stop();
            screenFive = null;
            showScreen(1);
        }
    }

    private void showScreen(int no) {
        if (no == 1) { // goto screenOne
            currentPage = 1;
            screenOne = new ScreenOne(this);
            screenOne.addCommand(cmdNext);
            screenOne.addCommand(cmdExit);
            screenOne.setCommandListener(this);
            display.setCurrent(screenOne);
            screenOne.start();
        } else if (no == 2) { // goto screenTwo
            currentPage = 2;
            screenTwo = new ScreenTwo(this);
            screenTwo.addCommand(cmdNext);
            screenTwo.addCommand(cmdExit);
            screenTwo.setCommandListener(this);
            display.setCurrent(screenTwo);
            screenTwo.start();
        } else if (no == 3) { // goto screenThree
            currentPage = 3;
            screenThree = new ScreenThree(this);
            screenThree.addCommand(cmdNext);
            screenThree.addCommand(cmdExit);
            screenThree.setCommandListener(this);
            display.setCurrent(screenThree);
            screenThree.start();
        } else if (no == 4) { // goto screenFour
            currentPage = 4;
            screenFour = new ScreenFour(this);
            screenFour.addCommand(cmdNext);
            screenFour.addCommand(cmdExit);
            screenFour.setCommandListener(this);
            display.setCurrent(screenFour);
            screenFour.start();
        } else if (no == 5) { // goto screenFive
            currentPage = 5;
            screenFive = new ScreenFive(this);
            screenFive.addCommand(cmdNext);
            screenFive.addCommand(cmdExit);
            screenFive.setCommandListener(this);
            display.setCurrent(screenFive);
            screenFive.start();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdNext) {
            if (currentPage == 1) {
                screenOne.stop();
                screenOne = null;
                showScreen(2);
            } else if (currentPage == 2) {
                screenTwo.stop();
                screenTwo = null;
                showScreen(3);
            } else if (currentPage == 3) {
                screenThree.stop();
                screenThree = null;
                showScreen(4);
            } else if (currentPage == 4) {
                screenFour.stop();
                screenFour = null;
                showScreen(5);
            } else if (currentPage == 5) {
                screenFive.stop();
                screenFive = null;
                showScreen(1);
            }
        }
        if (c == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        }
    }

    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }
}
